package com.lonty.intelligence.service;

import com.lonty.intelligence.mapper.BuildingMapper;
import com.lonty.intelligence.mapper.CertificateMapper;
import com.lonty.intelligence.model.Building;
import com.lonty.intelligence.request.BuildingByAreaIdList;
import com.lonty.intelligence.request.BuildingList;
import com.lonty.intelligence.util.UUIDUtil;
import com.lonty.intelligence.vo.BuildingVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author 陈璇
 * @Date 2020/4/17 13:19
 * @Version 1.0
 */
@Service
public class BuildingService {

    @Resource
    private BuildingMapper buildingMapper;

    @Resource
    private CertificateMapper certificateMapper;

    /**
     * 保存楼栋
     * @param buildingVo
     * @return
     */
    public Building saveBuilding(BuildingVo buildingVo){
        Building building = new Building();
        BeanUtils.copyProperties(buildingVo,building);
        building.setId(UUIDUtil.randomID());
        building.setIsDel(0);
        if(buildingMapper.saveBuilding(building) > 0){
            return building;
        }
        return null;
    }

    /**
     * 分页获取楼栋
     * @param areaId
     * @param page
     * @param size
     * @return
     */
    public BuildingList listBuilding(String areaId, Integer page, Integer size){
        List<Building> buildingList = buildingMapper.listBuilding(areaId, (page - 1) * size, page * size);
        return BuildingList.builder()
                .list(buildingList)
                .total(buildingMapper.countBuilding(areaId))
                .page(page)
                .build();
    }

    /**
     * 根据小区ID获取楼栋并且分组
     * @param areaId
     * @return
     */
    public List<BuildingByAreaIdList> getBuildingByAreaId(String areaId){
        return buildingMapper.getBuildingByAreaId(areaId);
    }

    /**
     * 判断楼栋是否存在
     * @param areaId
     * @param road
     * @param buildingNumber
     * @return
     */
    public List<Building> buildingIsExit(String areaId, String road, String buildingNumber){
        Building building = new Building();
        building.setAreaId(areaId);
        building.setRoad(road);
        building.setBuildingNumber(buildingNumber);
        return buildingMapper.buildingIsExit(building);
    }

    /**
     * 修改楼栋
     * @param id
     * @param type
     * @return
     */
    @Transactional
    public boolean modifyBuilding(String id, Integer type){
        certificateMapper.modifyCertificate(id, type);
        return buildingMapper.modifyBuilding(id, type) > 0;
    }

}
